/**
 *  2013-5-16   上午10:25:09
 *  Created By niexiaoqiang
 */

package com.xiaoqiang.network;

import com.xiaoqiang.object.MtwKey;
import com.xiaoqiang.object.MtwMouse;
import com.xiaoqiang.object.MtwObject;
import com.xiaoqiang.util.MsgRecivedEvent;
import com.xiaoqiang.util.MsgRecivedListener;
import com.xiaoqiang.util.MtwRobot;

/**
 * TODO Add Class Description
 */
public class MyMsgRecivedListener implements MsgRecivedListener {

	private MtwRobot robot=MtwRobot.getInstance();
	
	@Override
	public void msgRecivedEvent(MsgRecivedEvent event) {
		String msg=event.getMsgconent();
		Object obj=new MtwObject(msg).getObject();
		if( obj instanceof MtwKey)
		{
			keyAction((MtwKey)obj);
		}else
		{
			mouseAction((MtwMouse)obj);
		}
	}
	
	private void keyAction(MtwKey mtwKey)
	{
		if(mtwKey.getKeyAction()==0)
		{
			robot.pressKey(mtwKey.getKeyCode());
		}
		else
		{
			robot.releaseKey(mtwKey.getKeyCode());
		}
	}
	
	private void mouseAction(MtwMouse mtwMouse)
	{
		if(mtwMouse.getMouseAction().equals("MouseMove"))
		{
			robot.mouseMove((int)mtwMouse.getX(), (int)mtwMouse.getY());
		}else if(mtwMouse.getMouseAction().equals("MouseWheel"))
		{
			robot.mouseWheel((int)mtwMouse.getX());
		}else
		{
			if(mtwMouse.getmouseButtonAction()==0)
			{
				robot.mousePress(mtwMouse.getMouseButton());
			}
			else
			{
				robot.mousePress(mtwMouse.getMouseButton());
			}
		}
	}
}
